package com.example.Arithmetic.Lettcode;

import java.util.Collections;
import java.util.PriorityQueue;

/**
 * 日期：2023/10/13
 * 时间：14:09
 * 描述：数据流的中位数
 * 定义规则，左右相等左边加1，不等则右边加1
 * 左边添加取出右边最小，右边添加拿出左边最大
 */
public class E04Lettcode295 {
    PriorityQueue<Integer> left;
    PriorityQueue<Integer> right;

    public E04Lettcode295() {
        left = new PriorityQueue<>(Collections.reverseOrder());
        right = new PriorityQueue<>();
    }

    public void addNum(int num) {
        if (left.size() == right.size()) {
            right.offer(num);
            left.offer(right.poll());
        } else {
            left.offer(num);
            right.offer(left.poll());
        }
    }

    public double findMedian() {
        if (left.size()==right.size()){
            return (left.peek()+right.peek())/2.0;
        }else {
            return (double) left.peek();
        }

    }
}
